package com.daguchuangyi.erp_android.productlist.paramsfilter;

import android.content.Context;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bigkoo.pickerview.listener.CustomListener;
import com.bigkoo.pickerview.view.BasePickerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.daguchuangyi.erp_android.R;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by lijianjian on 2018/4/14.
 */

public class MultiPickerView extends BasePickerView {

    private int layoutRes;
    //    private CustomListener customListener;
    private TextView btnSubmit, btnCancel; //确定、取消按钮
    private TextView tvTitle;
    //    private RelativeLayout rv_top_bar;
    private int backgroundId; //显示时的外部背景色颜色,默认是灰色
    private CustomListener customListener;
    private String mTitleStr;
    private OptionsAdapter mOptionsAdapter;

    private OnOptionsSelectListener optionsSelectListener;

    private boolean isDialog;//是否是对话框模式

    private boolean cancelable;//是否能取消


    private List<String> mAllOptions;
    private Set<String> mCheckedOptions;

    //构造方法
    public MultiPickerView(Builder builder) {
        super(builder.context);
        this.optionsSelectListener = builder.optionsSelectListener;

        this.mTitleStr = builder.mTitle;
        this.layoutRes = builder.layoutRes;
        this.isDialog = builder.isDialog;
        this.decorView = builder.decorView;
        this.cancelable = true;
        this.mAllOptions = new ArrayList<>();
        this.mAllOptions.addAll(builder.mAllOptions);
        this.mCheckedOptions = new HashSet<>();
        this.mCheckedOptions.addAll(builder.mCheckedOptions);
        initView(builder.context);
    }


    //建造器
    public static class Builder {
        private int layoutRes = R.layout.multi_pickerview_options;
        private CustomListener customListener;
        private int backgroundId; //显示时的外部背景色颜色,默认是灰色
        private Context context;
        private OnOptionsSelectListener optionsSelectListener;
        private List<String> mAllOptions;
        private Set<String> mCheckedOptions;
        private String mTitle = "";

        public ViewGroup decorView;//显示pickerview的根View,默认是activity的根view
        // 条目间距倍数 默认1.6
        private boolean isDialog;//是否是对话框模式

        private Typeface font;

        //Required
        public Builder(Context context, OnOptionsSelectListener listener) {
            this.context = context;
            this.optionsSelectListener = listener;
        }

        public Builder isDialog(boolean isDialog) {
            this.isDialog = isDialog;
            return this;
        }

        public Builder setContent(List<String> contents, Set<String> checked) {
            this.mAllOptions = contents;
            this.mCheckedOptions = checked;
            return this;
        }

        public Builder setTitle(String title) {
            this.mTitle = title;
            return this;
        }

        public MultiPickerView build() {
            return new MultiPickerView(this);
        }
    }


    private void initView(Context context) {
        setDialogOutSideCancelable(cancelable);
        initViews(backgroundId);
        init();
        initEvents();
        if (customListener == null) {
            LayoutInflater.from(context).inflate(layoutRes, contentContainer);

            //顶部标题
            tvTitle = (TextView) findViewById(R.id.title_tv);
            tvTitle.setText(mTitleStr);
//            rv_top_bar = (RelativeLayout) findViewById(R.id.rv_topbar);

            //确定和取消按钮
            btnSubmit = (TextView) findViewById(R.id.i_confirm_tv);
            btnCancel = (TextView) findViewById(R.id.i_cancel_tv);

            btnSubmit.setOnClickListener(submitView -> {
                dismiss();
                returnData();
            });

            btnCancel.setOnClickListener(cancelTv -> dismiss());

            RecyclerView optionsRv = (RecyclerView) findViewById(R.id.options_rv);
            optionsRv.setLayoutManager(new LinearLayoutManager(context));
            mOptionsAdapter = new OptionsAdapter(mAllOptions, mCheckedOptions);
            optionsRv.setAdapter(mOptionsAdapter);

        } else {
            customListener.customLayout(LayoutInflater.from(context).inflate(layoutRes, contentContainer));
        }

        // ----滚轮布局
//        final LinearLayout optionsPicker = (LinearLayout) findViewById(R.id.optionspicker);
//        optionsPicker.setBackgroundColor(Color_Background_Wheel == 0 ? bgColor_default : Color_Background_Wheel);

        setOutSideCancelable(cancelable);

    }

    //抽离接口回调的方法
    public void returnData() {
        if (optionsSelectListener != null) {
            Set<String> checkedOptions = mOptionsAdapter.getCheckedOptions();
            optionsSelectListener.onOptionsSelect(checkedOptions);
//            int[] optionsCurrentItems = wheelOptions.getCurrentItems();
//            optionsSelectListener.onOptionsSelect(optionsCurrentItems[0], optionsCurrentItems[1], optionsCurrentItems[2], clickView);
        }
    }

    public interface OnOptionsSelectListener {
        void onOptionsSelect(Set<String> checkedOptions);
    }

    @Override
    public boolean isDialog() {
        return isDialog;
    }

    private class OptionsAdapter extends BaseQuickAdapter<String, BaseViewHolder> {

        private Set<String> mCheckedOptions;

        public Set<String> getCheckedOptions() {
            return mCheckedOptions;
        }

        public OptionsAdapter(@Nullable List<String> data, Set<String> checkedOptions) {
            super(R.layout.item_param_filter_option_layout, data);
            mCheckedOptions = new HashSet<>();
            if (checkedOptions != null) {
                this.mCheckedOptions.addAll(checkedOptions);
            }
        }

        @Override
        protected void convert(BaseViewHolder helper, String item) {
            helper.setText(R.id.title_tv, item);
            if (mCheckedOptions.contains(item)) {
                helper.getView(R.id.check_mark_iv).setVisibility(View.VISIBLE);
            } else {
                helper.getView(R.id.check_mark_iv).setVisibility(View.GONE);
            }
            helper.itemView.setOnClickListener(view -> {
                if (mCheckedOptions.contains(item)) {
                    mCheckedOptions.remove(item);
                    helper.getView(R.id.check_mark_iv).setVisibility(View.GONE);
                } else {
                    mCheckedOptions.add(item);
                    helper.getView(R.id.check_mark_iv).setVisibility(View.VISIBLE);
                }
            });
        }
    }
}
